package com.brk.socket.util;

import com.brk.socket.entity.Location;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author brk
 * @ateTime 2023/7/13 17:49
 * @Description
 */
@Slf4j
public class LocationInfoUtil {

    public static boolean checkLocation(Double[] polyX, Double[] polyY, Double x, Double y) {

        log.info("经度:" + x + " 纬度:" + y);
        log.info("围栏经度:" + Arrays.toString(polyX) + " 纬度:" + Arrays.toString(polyY));

        boolean oddNodes = false;
        int j = polyX.length - 1;

        for (int i = 0; i < polyX.length; i++) {
            if ((polyY[i] < y && polyY[j] >= y) || (polyY[j] < y && polyY[i] >= y) && (polyX[i] <= x || polyX[j] <= x)) {
                if (polyX[i] + (y - polyY[i]) / (polyY[j] - polyY[i]) * (polyX[j] - polyX[i]) < x) {
                    oddNodes = !oddNodes;
                }
            }
            j = i;
        }

        return oddNodes;
    }

    public static Location parseLocationString(String locationString) {
        //
        log.info(locationString);
        String[] locations = locationString.split(";");

        String longitude = locations[0];

        String latitude = locations[1];

        longitude = longitude.substring(10, longitude.length() - 2);

        latitude = latitude.substring(9, latitude.length() - 2);

        Location location = new Location();
        location.setX(Double.parseDouble(longitude));
        location.setY(Double.parseDouble(latitude));
        log.info("parseObj" + location.toString());

        return location;
    }

    public static void pareLocation(String xArr, String yArr, Location location) {
        xArr = xArr.substring(1, xArr.length() - 1);
        yArr = yArr.substring(1, yArr.length() - 1);

        String[] numXArr = xArr.split(",");
        String[] numYArr = yArr.split(",");

        List<Double> xList = new ArrayList<>();
        List<Double> yList = new ArrayList<>();
        for (int i = 0; i < numXArr.length; i++) {
            xList.add(Double.parseDouble(numXArr[i]));
            yList.add(Double.parseDouble(numYArr[i]));
        }
        Double[] x = new Double[xList.size()];
        Double[] y = new Double[yList.size()];

        location.setPolyX(xList.toArray(x));
        location.setPolyY(yList.toArray(y));
    }

    public static void main(String[] args) {
        Double[] x = new Double[]{113.40100957863694, 113.4016210231548, 113.40161795713033, 113.4011649498184};
        Double[] y = new Double[]{23.045003532730444, 23.04504392685579, 23.044644528103365, 23.044570749183457};

        System.out.println(checkLocation(x, y, 113.40139639150722, 23.04472476718517));
    }

}
